//
//  WSSearchFootView.m
//  CharonTVProject
//
//  Created by karong on 2023/8/1.
//

#import "WSSearchFootView.h"
#import "WSSearchHistoryCell.h"
#import "WSEqualSpaceFlowLayoutEvolve.h"

@interface WSSearchFootView () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) NSMutableArray * dataArray;
@property (strong ,nonatomic) WSEqualSpaceFlowLayoutEvolve *flowLayout;
@end

@implementation WSSearchFootView
- (void)awakeFromNib {
    [super awakeFromNib];
    self.flowLayout = [[WSEqualSpaceFlowLayoutEvolve alloc]initWthType:AlignWithLeft];
    self.flowLayout.betweenOfCell = 12;
    self.flowLayout.minimumLineSpacing = 12;
    self.listView.collectionViewLayout = self.flowLayout;
}

- (void)setDataWithObject:(id)object {
    self.dataArray = object;
    [self.listView reloadData];
}
#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WSSearchHistoryCell * searchHistoryCell;
    if (indexPath.row < 5) {
        searchHistoryCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSSearchHistoryCell" forIndexPath:indexPath];
        [searchHistoryCell setDataWithObject:self.dataArray[indexPath.row]];
    }
    else {
        searchHistoryCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSSearchHistoryDefaultCell" forIndexPath:indexPath];
        [searchHistoryCell setDataWithObject:self.dataArray[indexPath.row]];
    }
    return searchHistoryCell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    WSVideo *video = self.dataArray[indexPath.row];
    CGSize textSize = [video.title sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}];
    if (indexPath.row < 5) {
        size.width = MIN(ceil(textSize.width) + 20 + 20, kScreenWidth - 24);
    }
    else {
        size.width = MIN(ceil(textSize.width) + 20, kScreenWidth - 24);
    }
    size.height = 26;
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(searchFootViewWithObjct:)]) {
        [self.delegate searchFootViewWithObjct:self.dataArray[indexPath.row]];
    }
}

@end
